package cn.kgcnews.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.kgcnews.dao.CommentDao;
import cn.kgcnews.pojo.NewsComment;
import cn.kgcnews.utils.BaseDao;

public class CommentDaoImpl extends BaseDao implements CommentDao {

	@Override
	public int saveComment(NewsComment comment)throws Exception {
		String sql="INSERT INTO news_comments (cnid,ccontent,cip,cauthor)VALUES(?,?,?,?)";
		Object[] params={comment.getnId(),comment.getContext(),comment.getCip(),comment.getAuthorName()};
		int result = super.executeUpdate(sql, params);
		return result;
	}

	@Override
	public List<NewsComment> findAllComment(int cnid,int seff,int pagesize) throws Exception {
		ArrayList<NewsComment> list=new ArrayList<NewsComment>();
		String sql="SELECT cid,cnid, ccontent,cdate,cip,cauthor FROM news_comments WHERE cnid=? order by cdate DESC limit ?,?";
		Object[] params ={cnid,seff,pagesize};
		ResultSet rs = super.executeQuery(sql, params);
		while(rs!=null&&rs.next()){
			NewsComment com = new NewsComment();
			com.setAuthorName(rs.getString("cauthor"));
			com.setcId(rs.getInt("cid"));
			com.setCip(rs.getString("cip"));
			com.setContext(rs.getString("ccontent"));
			com.setDate(rs.getDate("cdate"));
			com.setnId(rs.getInt("cnid"));
			list.add(com);
		}
		//System.out.println("changduydddddddddddddddddddddd:"+list.size());
		return list;
	}

	@Override
	public int findsize(int cnid) throws Exception{
		String sql="select count(1) from news_comments where cnid=?";
		Object[] params={cnid};
		ResultSet rs  = super.executeQuery(sql, params) ;
		if(rs!=null&&rs.next()){
			if(rs.next()){
			return rs.getInt(1);
			}
		}
		return 0;
	}

}
